package com.springboot.original.service;

import com.springboot.original.common.utils.RedisUtil;
import com.springboot.original.domain.StudentAnswerQuestionRecord;
import com.springboot.original.domain.StudentAnswerQuestionRecordKey;
import com.springboot.original.domain.User;
import com.springboot.original.mapper.UserMapper;
import com.springboot.original.repository.StudentAnswerQuestionRecordRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @Auther: ZhangYongMing
 * @Date: 2018/11/15 14:47
 * @Description:
 */
@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;
    @Resource
    RedisUtil redisUtil;
    @Autowired
    private CassandraTemplate cassandraTemplate;
    @Autowired
    private StudentAnswerQuestionRecordRepository studentAnswerQuestionRecordRepository;

    public User selectByPrimaryKey(String id){
        return userMapper.selectByPrimaryKey(id);
    }
    public User selectByNickName(String name){
        redisUtil.set("aaa","bbb");
        return userMapper.selectByNickName(name);
    }

    public String getFromRedis(String key){
        return redisUtil.get(key)+"";
    }

    public void addForCassandra(StudentAnswerQuestionRecord studentAnswerQuestionRecord) {
        cassandraTemplate.insert(studentAnswerQuestionRecord);
    }
    public void truncateForCassandra() {
        cassandraTemplate.truncate(StudentAnswerQuestionRecord.class);
    }

    public List<StudentAnswerQuestionRecord> getAllForCassandra() {
        List<StudentAnswerQuestionRecord> questionRecords = studentAnswerQuestionRecordRepository.findAll();
        return questionRecords;
    }

    public StudentAnswerQuestionRecord updateForCassandra(StudentAnswerQuestionRecord studentAnswerQuestionRecord) {
        StudentAnswerQuestionRecord questionRecords = studentAnswerQuestionRecordRepository.save(studentAnswerQuestionRecord);
        return questionRecords;
    }

    public StudentAnswerQuestionRecord getByKeyForCassandra(StudentAnswerQuestionRecordKey studentAnswerQuestionRecordKey) {
        Optional<StudentAnswerQuestionRecord> questionRecords = studentAnswerQuestionRecordRepository.findById(studentAnswerQuestionRecordKey);
        return questionRecords.get();
    }


}
